<?php
//设置网页文件字符集
header("Content-type:text/html;charsetUTF-8");
//包含数据库连接文件
require_once 'Conn.php';
//设置数把库字对集，避免中文乱码
$db->query("SET NAMES utf8");
//处理用户提交的原始数据
function checkInput($data)
{
    $data = trim($data);//去除空格等不必要字得
    $data = stripslashes($data); //别除反剑杠
    $data = htmlspecialchars($data); //转义HTML特殊字符 
    return $data;
}
//获取学号
$stuNo = checkInput($_POST['stuNo']);
if (empty($stuNo)) {
    echo "<script>alert('课程号没有填写!');window.location='course.php';</script>";
    exit();
}
//检百学号是否已经存在
$sql = " SELECT * FROM course WHERE CourseID='$stuNo' ";
$result = $db->query($sql);
if ($result->num_rows > 0) {
    echo "<script>alert('该课程号已经存在!');window.location='course.php';</script> ";
    exit();
}
//获取姓名
$stuName = checkInput($_POST['stuName']);
if (empty($stuName)) {
    echo "<script>alert('课程没有填写!');window.location='course.php';</script>";
    exit();
}

    /*将学生信息添加到Student数据表*/
$sql = "INSERT INTO course VALUES('$stuNo','$stuName')";
$result = $db->query($sql);
if ($result) {
    echo "<script>alert('注册成功!');window.location='course.php';</script>";
    //echo "<script>alert('注册成功!');</script>";
} else {
    echo "<script>alert('注册失败!');window.location='course.php';</script>";
    //echo "<script>alert('注册失败!');</script>";
}